package com.welsy.user.command
{
	import com.adobe.cairngorm.commands.Command;
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.control.CairngormEvent;
	import mx.controls.Alert;
	import com.welsy.usermgt.business.GetSrcCriteriaDelegate;
	import com.welsy.usermgt.model.UserMgtModelLocator;
	import mx.collections.ArrayCollection;
	import com.adobe.cairngorm.model.ModelLocator;
	import com.welsy.util.NamespaceLocator;
	import com.welsy.user.business.GetUserItersDelegate;
	import com.welsy.user.event.GetUserItersEvent;
	import com.welsy.iter.vo.IterVO;
	import com.welsy.user.view.iterview.IterView;
	import com.welsy.user.model.UserModelLocator;
	import com.welsy.user.event.GetUserPendingCoursesEvent;
	import com.welsy.user.business.GetUserPendingCoursesDelegate;
	import com.welsy.user.vo.UserCourseVO;
	import com.welsy.iter.vo.iter.IterElementVO;
	import com.welsy.user.view.iterview.IterElementView;
	import com.welsy.user.view.iterview.IterElementView;
	
	public class GetUserPendingCoursesCommand implements Command, IResponder
	{
		
		private var umodel : UserModelLocator = UserModelLocator.getInstance();
		
		public function execute(event : CairngormEvent) : void {
			var delegate : GetUserPendingCoursesDelegate = new GetUserPendingCoursesDelegate(this);
			var eve : GetUserPendingCoursesEvent = GetUserPendingCoursesEvent(event);
			delegate.getPendingCourses(eve.userId, eve.status);
		}
		
		public function result( event : Object ) : void {
			
			var xres : XMLList = XMLList(event.result);
			
			var NS : Namespace = new Namespace(NamespaceLocator.TRACK_DATATYPES);
						
			var TAG_ITERELEMENTSSCOLLECTION : QName = new QName(NS, "IterElementsCollection");
			umodel.progress = parseUserCourses(xres.descendants(TAG_ITERELEMENTSSCOLLECTION).children());
			umodel.midView = umodel.progress;
			
		}
		
		public static function parseUserCourses(tiets : XMLList) : ArrayCollection/*UserCourseVO*/ {
			var tNS : Namespace = new Namespace(NamespaceLocator.TRACK_DATATYPES);
			var iNS : Namespace = new Namespace(NamespaceLocator.ITER_DATATYPES);
				
			var TAG_TRACKITERELEMENT : QName =  new QName(tNS, "TrackIterElementType");			
			var TAG_STATUS : QName = new QName(tNS, "Status");
			var TAG_ITERELEMENT : QName = new QName(tNS, "IterElement");
			var TAG_COURSE : QName = new QName(tNS, "Course");
			
			var TAG_ITERELEMENTID : QName = new QName(iNS, "IterElementId");
			var TAG_TITLE : QName = new QName(iNS, "Title");
			var TAG_DESCRIPTION : QName = new QName(iNS, "Description");
			var TAG_PARENTITERELEMENTID : QName = new QName(iNS, "ParentIterElementId");
			var TAG_COURSEID : QName = new QName(iNS, "CourseId");
			var TAG_FILEID : QName = new QName(iNS, "FileId");
			var TAG_FILENAME : QName = new QName(iNS, "FileName");
			var TAG_COURSEONSITEID : QName = new QName(iNS, "CourseOnSiteId");
			var TAG_CREDITSNUMBER : QName = new QName(iNS, "CreditsNumber");
			var TAG_TYPE : QName = new QName(iNS, "Type");
			var TAG_POSITION : QName = new QName(iNS, "Position");
			var TAG_PRIORITY : QName = new QName(iNS, "Priority");
			
			var coll : ArrayCollection = new ArrayCollection;
			
			for each(var tiet : XML in tiets) {
				var ucvobuf : UserCourseVO = new UserCourseVO;
				//Parse the status
				ucvobuf.status = tiet.child(TAG_STATUS);
				//Parse the iter element
				var ievobuf : IterElementVO = new IterElementVO;
				
				var iterElementList : XMLList = tiet.descendants(TAG_ITERELEMENT);
				
				ievobuf.iterElementId = iterElementList.child(TAG_ITERELEMENTID);
				ievobuf.title = iterElementList.child(TAG_TITLE);
				ievobuf.Description = iterElementList.child(TAG_DESCRIPTION);
				ievobuf.ParentIterElementId = iterElementList.child(TAG_PARENTITERELEMENTID);
				ievobuf.CourseId = iterElementList.child(TAG_COURSEID);
				ievobuf.fileId = iterElementList.child(TAG_FILEID);
				ievobuf.fileName = iterElementList.child(TAG_FILENAME);				
				ievobuf.CourseOnSiteId = iterElementList.child(TAG_COURSEONSITEID);
				ievobuf.CreditsNumber = iterElementList.child(TAG_CREDITSNUMBER);
				ievobuf.type = iterElementList.child(TAG_TYPE);
				ievobuf.position = iterElementList.child(TAG_POSITION);
				ievobuf.Priority = iterElementList.child(TAG_PRIORITY);
				
				ucvobuf.element = ievobuf;
				ucvobuf.course = null; //ATTENTION no parsing here
				
				var view : IterElementView = new IterElementView;
				view.userCourse = ucvobuf;
				coll.addItem(view);
			}
			return coll;
		}
		
		public function fault(event : Object) : void {
			Alert.show("GetUserPendingCoursesCommand.fault");
		}
		
	}
}